const moment = require('moment');
moment.locale('zh-cn');
module.exports = {
    async hook(hooks, ...args) {
        try {
            const h = await this.model('cmswing/hooks').hookscache(hooks);
            if (!think.isEmpty(h.ext)) {
                const ext = h.ext.split(',');
                const hookarr = [];
                for (const c of ext) {
                    // 查询插件状态
                    const status = await this.model('cmswing/ext').extcache(c, 'status');
                    if (Number(status) === 1) {
                        const ep = `ext/${c}/hooks`;
                        const Cls = this.controller(ep);
                        if (Number(h.type) === 1) {
                            hookarr.push(await Cls[hooks](...args));
                        } else {
                            return Cls[hooks](...args);
                        }
                    } else {
                        const models = await this.model('cmswing/model').get_model(null, null, { name: c });
                        // console.log(models);
                        if (!think.isEmpty(models)) {
                            const ep = `mod/${c}/hooks`;
                            const Cls = this.controller(ep);
                            if (Number(h.type) === 1) {
                                hookarr.push(await Cls[hooks](...args));
                            } else {
                                return Cls[hooks](...args);
                            }
                        }
                    }
                }
                const type = think._.last(args);
                if (think.isObject(type) && !think.isEmpty(type.$hook_key) && !think.isEmpty(type.$hook_type)) {
                    const cachehook = await think.cache(`hooks_${hooks}${type.$hook_type}${this.cookie('thinkjs')}`);
                    const hookobj = think.isEmpty(cachehook) ? {} : cachehook;
                    if (!think.isEmpty(hookarr)) {
                        hookobj[type.$hook_key] = hookarr.join('');
                    }
                    await think.cache(`hooks_${hooks}${type.$hook_type}${this.cookie('thinkjs')}`, hookobj);
                    return this.assign(`HOOKS@${hooks}@${type.$hook_type}`, await think.cache(`hooks_${hooks}${type.$hook_type}${this.cookie('thinkjs')}`));
                }
                if (think.isObject(type) && !think.isEmpty(type.$hook_type)) {
                    return this.assign(`HOOK@${hooks}@${type.$hook_type}`, hookarr.join(''));
                }
                if (think.isObject(type) && !think.isEmpty(type.$hook_key)) {
                    const hookobj = think.isEmpty(await think.cache(`hooks_${hooks}${this.cookie('thinkjs')}`)) ? {} : await think.cache(`hooks_${hooks}${this.cookie('thinkjs')}`);
                    if (!think.isEmpty(hookarr)) {
                        hookobj[type.$hook_key] = hookarr.join('');
                    }
                    await think.cache(`hooks_${hooks}${this.cookie('thinkjs')}`, hookobj);
                    return this.assign(`HOOKS@${hooks}`, await think.cache(`hooks_${hooks}${this.cookie('thinkjs')}`));
                }
                this.assign(`HOOK@${hooks}`, hookarr.join(''));
            }
        } catch (e) {
            think.logger.error(e);
        }
    },
}